package com.baidu.push.example;

import com.baidu.android.pushservice.PushConstants;

import edu.hnu.cloudflight.android.FlightNotificationActivity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Push消息处理receiver
 */
public class PushMessageReceiver extends BroadcastReceiver {
	/** TAG to Log */
	public static final String TAG = PushMessageReceiver.class.getSimpleName();

	AlertDialog.Builder builder;

	/**
	 * 
	 * 
	 * @param context
	 *            Context
	 * @param intent
	 *            接收的intent
	 */
	@Override
	public void onReceive(final Context context, Intent intent) {

		Log.d(TAG, ">>> Receive intent: \r\n" + intent.getStringExtra(PushConstants.EXTRA_CONTENT));
		Log.i(TAG, intent.getAction());
		if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
			// 获取消息内容
			String message = intent.getExtras().getString(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
			Intent toIntent = new Intent(context, FlightNotificationActivity.class);
			toIntent.putExtra("content", message);
			toIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(toIntent);

			// 用户自定义内容读取方式，CUSTOM_KEY值和管理界面发送时填写的key对应
			// String customContentString =
			// intent.getExtras().getString(CUSTOM_KEY);
			Log.i(TAG, "ACTION_MESSAGE: " + message);
		} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {

			String method = intent.getStringExtra(PushConstants.EXTRA_METHOD);
			// 方法返回错误码,您需要恰当处理。比如，方法为bind时，若失败，需要重新bind,即重新调用startWork
			int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE, PushConstants.ERROR_SUCCESS);
			// 返回内容
			String content = new String(intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT));

			Log.i(TAG, "ACTION_RECEIVE: " + method + errorCode + content);

		} else if (intent.getAction().equals(PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {

			String title = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);
			String content = intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);
			Log.i(TAG, "ACTION_RECEIVER_NOTIFICATION_CLICK: " + title + content);
			Intent toIntent = new Intent(context, FlightNotificationActivity.class);
			toIntent.putExtra("title", title);
			toIntent.putExtra("content", content);
			toIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(toIntent);

		}

	}
}
